﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Srvtools;
using System.Data;

public partial class WorkTimeN_MendWorkOn : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            spBtnSave2WorkOn.Visible = false;

            if (Request["id"] != null)
            {
                InitializeComponent(true);

                FLWebNavigator1.Visible = true;
                Master.DataSource = WMaster;
                WMaster.SetWhere("Id=" + Request["id"].ToString());


                 string FlowFlag = Request["FlowFlag"].ToString();
                 string EmployeeId = Request["EmpId"].ToString();
                if (FlowFlag == "" && Convert.ToInt32("0" + EmployeeId) == Convert.ToInt32(Srvtools.CliUtils.fLoginUser))
                {
                }
                else
                {
                    object o = FLWebNavigator1.NavControls["Comment"];
                   // FLWebNavigator1.NavControls.Clear();
                    FLWebNavigator1.NavControls.Add(o);
                }


                DataSet ds = WMaster.Execute("SELECT SYS_TODOLIST.LISTID FROM SYS_TODOLIST WHERE  SYS_TODOLIST.FORM_TABLE='WT_MendWorkOn'" +
                    " and SYS_TODOLIST.FORM_KEYS='Id' and  SYS_TODOLIST.FORM_PRESENTATION='Id=" + Request["id"].ToString() + "'");
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    ImageButton imb = new ImageButton();
                    imb.ImageUrl = "../image/uipics/Comment_C.gif";
                    imb.ID = "i";
                    imb.Attributes.Add("onclick", "window.open('../InnerPages/FlowComment.aspx?LISTID=" + ds.Tables[0].Rows[0][0].ToString()
                        + "&ATTACHMENTS=', '', 'resizable=yes,scrollbars=yes,width=645,height=400,top=100,left=200');return false");
                    FLWebNavigator1.Controls.Add(imb);
                }
            }
            else
            {
                InitializeComponent(false);
                Master.DataSource = WMaster;
            }
        }



        MasterPage_MasterPage mpg = (MasterPage_MasterPage)this.Page.Master;
        mpg.HelpHref = "";
        mpg.PageName = "考勤补时单";
        mpg.PicUrl = "";
        mpg.IntiBar();

    }

    private void InitializeComponent(bool action)
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WorkTimeN_MendWorkOn));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = action;
        this.WMaster.AlwaysClose = true;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "SWorkTime.WT_MendWorkOn";
        this.WMaster.ServerModify = true;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }

    object[] oDiff = null;
    protected object GetOnDuty()
    {
        //return Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + oDiff[1].ToString()).AddHours(1);
        return Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + oDiff[0].ToString());
    }

    protected object GetOffDuty()
    {
       // return Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + oDiff[0].ToString()).AddHours(-1);
        return Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + oDiff[1].ToString());
    }

    protected string GetEmpId()
    {
        String str = Srvtools.CliUtils.GetValue("_UserCode")[1].ToString();
        str = int.Parse(str).ToString();
        return str;
    }

    protected bool ValidateDateFrom(object value)
    {
        TextBox ZLTextBox1 = (TextBox)this.WebFormView1.FindControl("OnDutyTextBox");
        TextBox ZLTextBox2 = (TextBox)this.WebFormView1.FindControl("OffDutyTextBox");
        DateTime st = DateTime.Parse(ZLTextBox1.Text);
        DateTime et = DateTime.Parse(ZLTextBox2.Text);
        if (st.CompareTo(et) <= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    //protected bool Disp()
    //{
    //    if (Request.Params["A"] != null)
    //        return true;
    //    else
    //        return false;
    //}

    //protected void WebFormView1_DataBound(object sender, EventArgs e)
    //{
    //    if (this.WebFormView1.CurrentMode == FormViewMode.ReadOnly)
    //    {            
    //        if (Request.Params[""] != null)
    //        {
    //            Button btnMend = (Button)this.WebFormView1.FooterRow.FindControl("Button1");
    //            btnMend.Visible = true;
    //        }
    //    }
    //}
    //protected void WebFormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
    //{
    //    if (e.CommandName == "MendWorOn")
    //    {
    //        Srvtools.WebRefVal wrvEmpId = (Srvtools.WebRefVal)this.WebFormView1.Row.FindControl("EmpIdTextBox");
    //        BaseText.ZLTextBox ztbOnDuty = (BaseText.ZLTextBox)this.WebFormView1.Row.FindControl("OnDutyTextBox");
    //        BaseText.ZLTextBox ztbOffDuty = (BaseText.ZLTextBox)this.WebFormView1.Row.FindControl("OffDutyTextBox");
    //        if (wrvEmpId != null && ztbOnDuty != null && ztbOffDuty != null)
    //        {
    //            object[] objParam = new object[3];
    //            objParam[0] = wrvEmpId.BindingValue;
    //            objParam[1] = ztbOnDuty.GetDateTime.ToString();
    //            objParam[2] = ztbOffDuty.GetDateTime.ToString();

    //            object[] myRet = Srvtools.CliUtils.CallMethod("SWorkTime", "ExecMendWorkOn", objParam);
    //            if (myRet != null && (int)myRet[0] == 0)
    //            {
    //                bool boo = (bool)myRet[1];
    //                if (boo)
    //                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('已成功添加考勤数据')", true);
    //                else
    //                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('添加失败！请联系管理员！')", true);
    //            }
    //        }
    //    }
    //}
    protected void BtnSave2WorkOn_Click(object sender, EventArgs e)
    {
        Srvtools.WebRefVal wrvEmpId = (Srvtools.WebRefVal)this.WebFormView1.Row.FindControl("EmpIdTextBox");
        BaseText.ZLTextBox ztbOnDuty = (BaseText.ZLTextBox)this.WebFormView1.Row.FindControl("OnDutyTextBox");
        BaseText.ZLTextBox ztbOffDuty = (BaseText.ZLTextBox)this.WebFormView1.Row.FindControl("OffDutyTextBox");
        if (wrvEmpId != null && ztbOnDuty != null && ztbOffDuty != null)
        {
            object[] objParam = new object[3];
            objParam[0] = wrvEmpId.BindingValue;
            objParam[1] = ztbOnDuty.GetDateTime.ToString();
            objParam[2] = ztbOffDuty.GetDateTime.ToString();

            object[] myRet = Srvtools.CliUtils.CallMethod("SWorkTime", "ExecMendWorkOn", objParam);
            if (myRet != null && (int)myRet[0] == 0)
            {
                bool boo = (bool)myRet[1];
                if (boo)
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('已成功添加考勤数据')", true);
                else
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('添加失败！请联系管理员！')", true);
            }
        }

    }

    protected void FLWebNavigator1_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "cmdAdd")
        {
            object[] myRet = CliUtils.CallMethod("SWorkTime", "GetWorkOnType", new object[1] { "WorkTime_WorkOnTypeGroup.EmployeeId=" + Convert.ToInt32(Srvtools.CliUtils.GetValue("_UserCode")[1].ToString()) });

            switch (myRet[1].ToString())
            {
                case "N":
                case "S":
                    oDiff = new object[] { ((DataSet)myRet[2]).Tables[0].Rows[0]["ect"].ToString(), ((DataSet)myRet[2]).Tables[0].Rows[0]["st"].ToString() };
                    break;
                default:
                    oDiff = new object[] { "08:30", "17:30" };
                    break;
            }
        }
        else if (e.CommandName == "cmdApply")
        {
            if (IsPostBack == true)
            {
                if (Request["id"] != null)
                {
                    //Master.Reload();
                    Master.SetWhere("Id=" + Request["id"].ToString());
                }
                else
                {
                    //Master.Reload();
                }
            }

        }
    }
}
